//
//  NewsDetailViewController.m
//  Menu
//
//  Created by Lynn-Shin on 15/7/3.
//  Copyright (c) 2015年 lijiangang. All rights reserved.
//

#import "NewsDetailViewController.h"

@interface NewsDetailViewController ()

@end

@implementation NewsDetailViewController

-(void)dealloc{
    
    [_preferCollectV release];
    [_bigImage release];
    [_titleOneLabel release];
    [_titleTwoLabel release];
    [_contentTextV release];
    [_preferArray release];
    [_preferModel release];
    [_HUD release];
    
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    flowLayout.itemSize = CGSizeMake(160*WIDTH, 120*HEIGHT);
    flowLayout.headerReferenceSize = CGSizeMake(375*WIDTH, 667*HEIGHT-64-49);
    flowLayout.sectionInset = UIEdgeInsetsMake(10*HEIGHT, 15*WIDTH, 10*HEIGHT, 15*WIDTH);
    flowLayout.minimumInteritemSpacing = 10;
    flowLayout.minimumLineSpacing = 10;
    
    self.preferCollectV = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 64, 375*WIDTH, 667*HEIGHT-64-49) collectionViewLayout:flowLayout];
    self.preferCollectV.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.preferCollectV];
    [flowLayout release];
    [self.preferCollectV release];
    
    self.preferCollectV.delegate = self;
    self.preferCollectV.dataSource = self;
    
    [self.preferCollectV registerClass:[PreferCollectionViewCell class] forCellWithReuseIdentifier:@"prefer"];
    [self.preferCollectV registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"headerReuse"];
    
    self.HUD = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    self.HUD.labelText = @"loading";
    [self.HUD show:YES];
    
    self.preferArray = [NSMutableArray array];
    
    [self downLoadData];
    
    // Do any additional setup after loading the view.
}


-(void)downLoadData{
    
    NSString *getUrl = [NSString stringWithFormat:@"http://121.41.117.95:80/HandheldKitchen/api/found/tblFresh!getTblFreshDelicacyList.do?freshId=%ld&is_traditional=0&phonetype=1",self.freshId];
    
    [AFNetWorkingYU netWorkingHanderGetGataWithUrl:getUrl resultBlock:^(id result) {
        
        NSMutableArray *array = [result objectForKey:@"data"];
        
        self.preferModel = [[[PreferModel alloc] initWithDic:[array objectAtIndex:0]] autorelease];
        
        NSMutableArray *downArray = [[array firstObject]objectForKey:@"vegetable"];
        for (NSMutableDictionary *dic in downArray) {
            
            PreferModel *preferModel = [[PreferModel alloc] initWithDic:dic];
            [self.preferArray addObject:preferModel];
            [preferModel release];
        }
        
        [self.preferCollectV reloadData];
        [self.HUD hide:YES];
    }];
}

-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    
    return 1;
}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    
    return self.preferArray.count;
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    PreferCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"prefer" forIndexPath:indexPath];
    
    cell.preferModel = [self.preferArray objectAtIndex:indexPath.item];
    
    return cell;
}

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    
    CookingViewController *cookingVC = [[CookingViewController alloc] init];
    PreferModel *prefer = [self.preferArray objectAtIndex:indexPath.item];
    cookingVC.vegetable_id = prefer.vegetable_id;
    cookingVC.foodName = prefer.name;
    cookingVC.imagePathThumbnails = prefer.imagePathThumbnails;
    
    [self.navigationController pushViewController:cookingVC animated:YES];
    [cookingVC release];
}

//-----collectView的HeaderView-----
-(UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{
    
    if (kind == UICollectionElementKindSectionHeader) {
        
        UICollectionReusableView *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"headerReuse" forIndexPath:indexPath];
        headerView.backgroundColor = [UIColor whiteColor];
        
        self.bigImage = [[UIImageView alloc] initWithFrame:CGRectMake(5*WIDTH, 5*HEIGHT, 365*WIDTH, 260*HEIGHT)];
//        self.bigImage.backgroundColor = [UIColor redColor];
        [self.bigImage sd_setImageWithURL:[NSURL URLWithString:self.preferModel.imageFilename]];
        [headerView addSubview:self.bigImage];
        [self.bigImage release];
        
        self.titleOneLabel = [[UILabel alloc] initWithFrame:CGRectMake(20*WIDTH, 270*HEIGHT, 100*WIDTH, 40*HEIGHT)];
//        self.titleOneLabel.backgroundColor = [UIColor lightGrayColor];
        self.titleOneLabel.text = @"文字介绍";
        self.titleOneLabel.font = [UIFont systemFontOfSize:20];
        [headerView addSubview:self.titleOneLabel];
        [self.titleOneLabel release];
        
        self.contentTextV = [[UITextView alloc] initWithFrame:CGRectMake(10*WIDTH, 305*HEIGHT, 355*WIDTH, 210*HEIGHT)];
//        self.contentTextV.backgroundColor = [UIColor lightGrayColor];
        self.contentTextV.font = [UIFont systemFontOfSize:17];
        self.contentTextV.editable = NO;
        self.contentTextV.text = self.preferModel.content;
        [headerView addSubview:self.contentTextV];
        [self.contentTextV release];
        
        self.titleTwoLabel = [[UILabel alloc] initWithFrame:CGRectMake(20*WIDTH, 517*HEIGHT, 100*WIDTH, 40*HEIGHT)];
//        self.titleTwoLabel.backgroundColor = [UIColor lightGrayColor];
        self.titleTwoLabel.text = @"推荐菜品";
        self.titleTwoLabel.font = [UIFont systemFontOfSize:20];
        [headerView addSubview:self.titleTwoLabel];
        [self.titleTwoLabel release];
        
        return headerView;
        
    }else{
        
        UICollectionReusableView *footerView = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"footerReuse" forIndexPath:indexPath];
        footerView.backgroundColor = [UIColor blueColor];
        return footerView;
    }
    //kind是显示区域类型
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
